package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.dao.TopicDao;
import com.ruoyi.system.domain.dto.TopicDto;
import com.ruoyi.system.domain.po.*;
import com.ruoyi.system.domain.vo.TopicVo;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.service.TopicService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class TopicServiceImpl implements TopicService {

    @Resource
    private TopTopicMapper topTopicMapper;
    @Resource
    private TopicDao topicDao;
    @Resource
    private TopEntryConditionsMapper entryConditionsMapper;
    @Resource
    private TopResearchPathMapper topResearchPathMapper;
    @Resource
    private CmsArchivesTagMapper cmsArchivesTagMapper;
    @Resource
    private TopGroupPatientMapper topGroupPatientMapper;
    @Resource
    private PmsPatientMapper pmsPatientMapper;

    @Override
    public void add(TopicDto.Insert dto) {
        TopTopic topTopic = BeanUtil.copyProperties(dto, TopTopic.class);
        Long id = dto.getId();
        if (id == null) {
            topTopic.setId(IdUtil.getSnowflake().nextId());
            topTopic.setCreateTime(new Date());
            topTopic.setCreateBy(SecurityUtils.getUsername());
            topTopic.setDelFlag((byte) 0);
            topTopicMapper.insertSelective(topTopic);
        } else {
            topTopic.setUpdateTime(new Date());
            topTopic.setUpdateBy(SecurityUtils.getUsername());
            topTopicMapper.updateByPrimaryKeySelective(topTopic);
        }
    }

    @Override
    public void delete(TopicDto.Delete dto) {
        TopTopicExample topTopicExample = new TopTopicExample();
        topTopicExample.createCriteria().andIdIn(dto.getIdList());
        TopTopic topTopic = new TopTopic();
        topTopic.setDelFlag((byte) 1);
        topTopicMapper.updateByExampleSelective(topTopic, topTopicExample);
    }

    @Override
    public List<TopicVo.Result> query(TopicDto.Query dto) {
        return topicDao.query(dto);
    }

    @Override
    public TopicVo.TopEntryConditionsResult queryEntryConditionsResult(TopicDto.TopEntryConditionsQuery dto) {
        TopEntryConditionsExample topEntryConditionsExample = new TopEntryConditionsExample();
        topEntryConditionsExample.createCriteria().andDelFlagEqualTo((byte) 0).andTopicIdEqualTo(dto.getTopicId());
        List<TopEntryConditions> topEntryConditions = entryConditionsMapper.selectByExample(topEntryConditionsExample);
        if (CollectionUtil.isNotEmpty(topEntryConditions)) {
            return BeanUtil.copyProperties(topEntryConditions.get(0), TopicVo.TopEntryConditionsResult.class);
        }
        return new TopicVo.TopEntryConditionsResult();
    }

    @Override
    public void deleteEntryConditionsResult(TopicDto.TopEntryConditionsDelete dto) {
        TopEntryConditionsExample topEntryConditionsExample = new TopEntryConditionsExample();
        topEntryConditionsExample.createCriteria().andIdIn(dto.getIdList());
        TopEntryConditions topEntryConditions = new TopEntryConditions();
        topEntryConditions.setDelFlag((byte) 1);
        entryConditionsMapper.updateByExampleSelective(topEntryConditions, topEntryConditionsExample);
    }

    @Override
    public void addEntryConditionsResult(TopicDto.TopEntryConditionsInsert dto) {
        TopEntryConditionsExample topEntryConditionsExample = new TopEntryConditionsExample();
        topEntryConditionsExample.createCriteria().andTopicIdEqualTo(dto.getTopicId()).andDelFlagEqualTo((byte) 0);
        List<TopEntryConditions> topEntryConditions = entryConditionsMapper.selectByExample(topEntryConditionsExample);
        if (CollectionUtil.isNotEmpty(topEntryConditions)) {
            topEntryConditions.forEach(e -> entryConditionsMapper.deleteByPrimaryKey(e.getId()));
        }

        TopEntryConditions topEntryConditions1 = BeanUtil.copyProperties(dto, TopEntryConditions.class);
        topEntryConditions1.setDelFlag((byte) 0);
        topEntryConditions1.setCreateBy(SecurityUtils.getUsername());
        topEntryConditions1.setCreateTime(new Date());
        topEntryConditions1.setId(IdUtil.getSnowflake().nextId());
        entryConditionsMapper.insertSelective(topEntryConditions1);
    }

    @Override
    public void addResearchPath(TopicDto.TopResearchPathAdd dto) {

        TopResearchPathExample topResearchPathExample1 = new TopResearchPathExample();
        topResearchPathExample1.createCriteria().andTopicIdEqualTo(dto.getTopicId());
        TopResearchPath topResearchPath1 = new TopResearchPath();
        topResearchPath1.setDelFlag((byte) 1);
        topResearchPathMapper.updateByExampleSelective(topResearchPath1, topResearchPathExample1);

        for (Long archivesTagId : dto.getArchivesTagIdList()) {
//            TopResearchPathExample topResearchPathExample = new TopResearchPathExample();
//            topResearchPathExample.createCriteria().andDelFlagEqualTo((byte) 0).andTopicIdEqualTo(dto.getTopicId()).andArchivesTagIdEqualTo(archivesTagId);
//            List<TopResearchPath> topResearchPaths = topResearchPathMapper.selectByExample(topResearchPathExample);
//            if (CollectionUtil.isEmpty(topResearchPaths)) {
                TopResearchPath topResearchPath = new TopResearchPath();
                topResearchPath.setTopicId(dto.getTopicId());
                topResearchPath.setId(IdUtil.getSnowflake().nextId());
                topResearchPath.setArchivesTagId(archivesTagId);
                topResearchPath.setDelFlag((byte) 0);
                topResearchPath.setCreateBy(SecurityUtils.getUsername());
                topResearchPathMapper.insertSelective(topResearchPath);
//            }
        }
    }

    @Override
    public void deladdResearchPath(TopicDto.TopResearchPathDelete dto) {
        TopResearchPathExample topResearchPathExample = new TopResearchPathExample();
        topResearchPathExample.createCriteria().andIdIn(dto.getIdList());
        TopResearchPath topResearchPath = new TopResearchPath();
        topResearchPath.setDelFlag((byte) 0);
        topResearchPathMapper.updateByExampleSelective(topResearchPath, topResearchPathExample);
    }

    @Override
    public List<TopicVo.TopResearchPathResult> queryResearchPath(TopicDto.TopResearchPathQuery dto) {
        //查询菜单
        CmsArchivesTagExample cmsArchivesTagExample = new CmsArchivesTagExample();
        cmsArchivesTagExample.createCriteria().andDelFlagEqualTo((byte) 0);
        List<CmsArchivesTag> cmsArchivesTags = cmsArchivesTagMapper.selectByExample(cmsArchivesTagExample);
        Map<Long, CmsArchivesTag> map = new HashMap<>();
        if (CollectionUtil.isNotEmpty(cmsArchivesTags)) {
            map = cmsArchivesTags.stream().collect(Collectors.toMap(CmsArchivesTag::getId, Function.identity()));
        }
        List<TopicVo.TopResearchPathResult> list = new ArrayList<>();
        //查询课题科研路径
        TopResearchPathExample topResearchPathExample = new TopResearchPathExample();
        topResearchPathExample.createCriteria().andTopicIdEqualTo(dto.getTopicId()).andDelFlagEqualTo((byte) 0);
        List<TopResearchPath> topResearchPaths = topResearchPathMapper.selectByExample(topResearchPathExample);
        if (CollectionUtil.isNotEmpty(topResearchPaths)) {
            Map<Long, List<TopResearchPath>> collect = topResearchPaths.stream().collect(Collectors.groupingBy(TopResearchPath::getTopicId));
            for (Map.Entry<Long, List<TopResearchPath>> entry : collect.entrySet()) {
                TopicVo.TopResearchPathResult topResearchPathResult = new TopicVo.TopResearchPathResult();
                topResearchPathResult.setTopicId(entry.getKey());
                List<Long> tagIdList = entry.getValue().stream().map(TopResearchPath::getArchivesTagId).collect(Collectors.toList());
                List<TopicVo.TopResearchPathResult.TagVo> tagVoList = new ArrayList<>();
                if (CollectionUtil.isNotEmpty(tagIdList)) {
                    for (Long tagId : tagIdList) {
                        TopicVo.TopResearchPathResult.TagVo tagVo = new TopicVo.TopResearchPathResult.TagVo();
                        CmsArchivesTag cmsArchivesTag = map.get(tagId);
                        if (cmsArchivesTag != null) {
                            tagVo.setArchivesTagName(cmsArchivesTag.getName());
                            tagVo.setType(cmsArchivesTag.getType());
                        }
                        tagVo.setArchivesTagId(tagId);
                        tagVoList.add(tagVo);
                    }
                }
                topResearchPathResult.setTagVoList(tagVoList);
                list.add(topResearchPathResult);
            }
        }
        return list;
    }

    @Override
    public List<TopicVo.Detail> queryDetailByTopic(Long topicId, Byte status, Integer pageNum, Integer pageSize) {
        //查询入组条件
        TopEntryConditionsExample topEntryConditionsExample = new TopEntryConditionsExample();
        topEntryConditionsExample.createCriteria().andTopicIdEqualTo(topicId).andDelFlagEqualTo((byte) 0);
        List<TopEntryConditions> topEntryConditionsList = entryConditionsMapper.selectByExample(topEntryConditionsExample);
        TopEntryConditions topEntryConditions = new TopEntryConditions();
        if (CollectionUtil.isNotEmpty(topEntryConditionsList)) {
            topEntryConditions = topEntryConditionsList.get(0);
        }
        TopGroupPatientExample topGroupPatientExample = new TopGroupPatientExample();
        topGroupPatientExample.createCriteria().andTopicIdEqualTo(topicId).andDelFlagEqualTo((byte) 0);
        List<TopGroupPatient> topGroupPatients = topGroupPatientMapper.selectByExample(topGroupPatientExample);
        List<Long> collect = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(topGroupPatients)) {
            collect = topGroupPatients.stream().map(TopGroupPatient::getPatientId).collect(Collectors.toList());
        }
        if (status == 1) {
            if (CollectionUtil.isNotEmpty(collect)) {
                PmsPatientExample patientExample = new PmsPatientExample();
                patientExample.createCriteria().andIdIn(collect).andDelFlagEqualTo((byte) 0);
                List<PmsPatient> patients = pmsPatientMapper.selectByExample(patientExample);
                if (CollectionUtil.isNotEmpty(patients)) {
                   return BeanUtil.copyToList(patients, TopicVo.Detail.class);
                }
            }
            return CollectionUtil.newArrayList();
        }
        //根据入组条件查询患者数据
        PageHelper.startPage(pageNum, pageSize);
        return topicDao.queryDetail(topicId, status, topEntryConditions, collect);
    }

    @Override
    public void addGroup(TopicDto.GroupInsert dto) {
        TopGroupPatientExample topGroupPatientExample = new TopGroupPatientExample();
        topGroupPatientExample.createCriteria().andTopicIdEqualTo(dto.getTopicId()).andPatientIdEqualTo(dto.getPatientId()).andDelFlagEqualTo((byte) 0);
        List<TopGroupPatient> topGroupPatients = topGroupPatientMapper.selectByExample(topGroupPatientExample);
        if (CollectionUtil.isEmpty(topGroupPatients)) {
            TopGroupPatient topGroupPatient = new TopGroupPatient();
            topGroupPatient.setCreateBy(SecurityUtils.getUsername());
            topGroupPatient.setId(IdUtil.getSnowflake().nextId());
            topGroupPatient.setTopicId(dto.getTopicId());
            topGroupPatient.setPatientId(dto.getPatientId());
            topGroupPatientMapper.insertSelective(topGroupPatient);
        }
    }

    @Override
    public void delGroup(TopicDto.GroupDelete dto) {
        TopGroupPatient topGroupPatient = new TopGroupPatient();
        topGroupPatient.setDelFlag((byte) 1);
        TopGroupPatientExample topGroupPatientExample = new TopGroupPatientExample();
        topGroupPatientExample.createCriteria().andPatientIdEqualTo(dto.getPatientId()).andTopicIdEqualTo(dto.getTopicId());
        topGroupPatientMapper.updateByExampleSelective(topGroupPatient, topGroupPatientExample);
    }
}
